package com.youko.backstage.web;


import com.youko.backstage.bean.Merchant;
import com.youko.backstage.dto.merchant.MerchantLoginDto;
import com.youko.backstage.dto.merchant.MerchantLoginResponseDto;
import com.youko.backstage.dto.merchant.MerchantRegisterDto;
import com.youko.backstage.dto.merchant.ResponseDto;
import com.youko.backstage.mapper.MerchantMapper;
import com.youko.backstage.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Value("${SavePath.ProfilePhoto}")
    private String ProfilePhotoSavePath;   //图标图片存储路径
    @Value("${SavePath.ProfilePhotoMapper}")
    private String ProfilePhotoMapperPath;   //图标映射路径

    /**注册接口*/
    @PostMapping("/merchant/register")
    public ResponseDto saveMerchant(@RequestBody MerchantRegisterDto merchantRegisterDto){
        ResponseDto responseDto=new ResponseDto();
        if (merchantRegisterDto==null){
            responseDto.setMsg("注册信息缺失");
            responseDto.setStatus(0);
            return responseDto;
        }
        if(merchantRegisterDto.getName()==null||merchantRegisterDto.getPassword()==null){
            responseDto.setMsg("注册账号或密码缺失");
            responseDto.setStatus(0);
            return responseDto;
        }
        Merchant merchant=new Merchant();
        merchant.setName(merchantRegisterDto.getName());
        merchant.setPassword(passwordEncoder.encode(merchantRegisterDto.getPassword()));
        merchant.setMobile(merchantRegisterDto.getMobile());
        if (merchantService.getMerchant(merchantRegisterDto.getName())!=null){
            responseDto.setMsg("此账号重复");
            responseDto.setStatus(0);
            return responseDto;
        }
        merchantService.saveMerchant(merchant);
        responseDto.setMsg("注册成功");
        responseDto.setStatus(1);
        return responseDto;
    }

    /**登陆接口*/
    @PostMapping("/merchant/login")
    public ResponseDto loginIn(@RequestBody MerchantLoginDto merchantLoginDto){
        ResponseDto responseDto=new ResponseDto();
        MerchantLoginResponseDto merchantLoginResponseDto=new MerchantLoginResponseDto();
        Merchant merchant= merchantService.getMerchant(merchantLoginDto.getName());
        if (merchantLoginDto==null){
            responseDto.setMsg("登陆信息缺失");
            responseDto.setStatus(0);
            return responseDto;
        }
        if (merchant==null){
            responseDto.setMsg("查无此号");
            responseDto.setStatus(0);
            return responseDto;
        }
        if (!passwordEncoder.matches(merchantLoginDto.getPassword(),merchant.getPassword())){
            responseDto.setMsg("密码错误");
            responseDto.setStatus(0);
            return responseDto;
        }
        merchantLoginResponseDto.setMsg("登陆成功");
        merchantLoginResponseDto.setStatus(1);
        merchantLoginResponseDto.setId(merchant.getId());
        merchantLoginResponseDto.setName(merchant.getName());
        merchantLoginResponseDto.setNickname(merchant.getNickname());
        merchantLoginResponseDto.setPortrait(merchant.getAvatar());
        return merchantLoginResponseDto;
    }

    /**修改密码接口*/
    @PutMapping("/merchant/updatePassword")
    public ResponseDto updatePassword(@RequestParam int id,@RequestParam String newPassword){
        merchantService.updatePassword(id,newPassword);
        ResponseDto responseDto=new ResponseDto();
        responseDto.setStatus(1);
        responseDto.setMsg("修改密码成功");
        return responseDto;
    }


    @PostMapping("/merchant/profilePhotoUpload")
    public String profilePhotoUpload(@RequestParam int id, @RequestParam("file") MultipartFile fileUpload){
        //获取文件名
        String fileName = fileUpload.getOriginalFilename();
        //获取文件后缀名。也可以在这里添加判断语句，规定特定格式的图片才能上传，否则拒绝保存。
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //为了避免发生图片替换，这里使用了文件名重新生成
        fileName = UUID.randomUUID()+suffixName;
        try {
            //将图片保存到文件夹里
            fileUpload.transferTo(new File(ProfilePhotoSavePath+fileName));
            merchantService.updateMerchantPhotoPath(id,ProfilePhotoMapperPath+fileName);
            return ProfilePhotoMapperPath+fileName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


}
